package test.flex.rtmp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import kr.oss.cho.mapper.SqlVeiwMapper;
import kr.oss.cho.util.SqlSessionFactoryUtil;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import test.oss.mapper.DepartmentsMapper;

import flex.messaging.MessageBroker;
import flex.messaging.messages.AsyncMessage;
import flex.messaging.util.UUIDUtils;

public class Feed {
	
	private static FeedThread thread;
	
	public Feed() {}
	
	/**
	 * Feed start
	 * */
	public void started() {
		System.out.println("sysStart");
		if( thread == null ) {
			thread = new FeedThread();
			thread.start();
		} 
	}
	
	/**
	 * Feed Stop
	 * */
	public void stopped() {
		thread.running = false;
		thread = null;
	}
	
	public static class FeedThread extends Thread {
		public boolean running = true;
		
		public void run() {
			MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
			String clientID = UUIDUtils.createUUID(false);
			
			Random random = new Random();
			int myInt;
			int row;
			int col;
			
			while( running ) {
				myInt = random.nextInt(10000);
				row = random.nextInt(20);
				col = random.nextInt(10);
				
				AsyncMessage msg = new AsyncMessage();
				msg.setDestination("testDataFeed");
				msg.setClientId(clientID);
				msg.setMessageId(UUIDUtils.createUUID(false));
				msg.setTimestamp(System.currentTimeMillis());
				

				SqlSessionFactory sqlMapper =  SqlSessionFactoryUtil.getInstance();
				
				SqlSession session = sqlMapper.openSession();
				
				Integer count = 0;
				try{
					SqlVeiwMapper mapper = session.getMapper(SqlVeiwMapper.class);
					
					//count = mapper.selectSessionCount();
					
				}catch(Exception e){
					System.out.println(e.toString());
				}finally{
					session.close();
				}
				
				msg.setBody(count);

//				msg.setBody(new FeedBody(myInt, row, col));
				
				msgBroker.routeMessageToService(msg, null);
				
				try {
					Thread.sleep(100);
				} catch(InterruptedException e) {
				}
			}
		}
	}
	
	public static class FeedBody implements Serializable {
		public int value;
		public int row;
		public int col;
		public FeedBody(int value, int row, int col) {
			this.value = value;
			this.row = row;
			this.col = col;
		}
	}
	public void grid(){
		System.out.println("grid()");
		MessageBroker msgBroker = MessageBroker.getMessageBroker(null);
		String clientID = UUIDUtils.createUUID(false);
		
		AsyncMessage msg = new AsyncMessage();
		msg.setDestination("testDataFeed");
		msg.setClientId(clientID);
		msg.setMessageId(UUIDUtils.createUUID(false));
		msg.setTimestamp(System.currentTimeMillis());
		
		
		System.out.println("test 1");
		SqlSessionFactory sqlMapper =  SqlSessionFactoryUtil.getInstance();
		System.out.println("test 2");
		
		SqlSession session = sqlMapper.openSession();
		System.out.println("test 3");
		
		ArrayList list= new ArrayList();
		try{
			System.out.println("test 4");
			DepartmentsMapper mapper = session.getMapper(DepartmentsMapper.class);
			System.out.println("test 5");
//			list=  mapper.selectViewSql();
			list = mapper.selectViewSql();
			
			System.out.println(list.size());
			System.out.println(list);
			Iterator it =  list.iterator();
			while(it.hasNext()){
				System.out.println("it:"+it.next());
			}
			
			System.out.println(":"+list.isEmpty());
			System.out.println(":"+list.get(0));

		}catch(Exception e){
			System.out.println(e.toString());
		}finally{
			session.close();
		}
		
		
//		Map<String, String> map = new HashMap<String, String>();
//		map.put("sql", "1select * from aa");
//		map.put("sql_id", "adslfkj");
//		
//		List list = new ArrayList();
//		list.add(map);
//		map = new HashMap<String, String>();
//		map.put("sql", "2select * from aa");
//		map.put("sql_id", "adslfkj");
//		list.add(map);
//		map = new HashMap<String, String>();
//		map.put("sql", "3select * from aa");
//		map.put("sql_id", "adslfkj");
//		list.add(map);
//		
		msg.setBody(list);
		msgBroker.routeMessageToService(msg, null);
	}
}
